package com.beiben.ability.people.service.impl;

import com.beiben.ability.people.domain.Details;
import com.beiben.ability.people.mapper.DetailsMapper;
import com.beiben.ability.people.service.IDetailsService;
import com.beiben.system.user.domain.BeiBenUser;
import com.beiben.system.user.mapper.BeiBenUserMapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 考试详情Service业务层处理
 *
 * @author dulei
 * @date 2020-08-26
 */
@Service
public class DetailsServiceImpl implements IDetailsService {

    @Autowired
    private DetailsMapper detailsMapper;


    @Autowired
    private SysDictDataMapper dataMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private BeiBenUserMapper beiBenUserMapper;

    /**
     * 查询考试详情
     *
     * @param detailsId 考试详情ID
     * @return 考试详情
     */
    @Override
    public Details selectDetailsById(Long detailsId) {
        return detailsMapper.selectDetailsById(detailsId);
    }

    /**
     * 查询考试详情列表
     *
     * @param details 考试详情
     * @return 考试详情
     */
    @Override
    public List<Details> selectDetailsList(Details details) {
        return detailsMapper.selectDetailsList(details);
    }

    /**
     * 新增考试详情
     *
     * @param details 考试详情
     * @return 结果
     */
    @Override
    public int insertDetails(Details details) {
        if(null!=details.getUserId()){
            // 验证考试信息：人员、考试记录是否存在
            int count = detailsMapper.checkDetailExist(details);
            //检查成绩是否存在
            checkExamIsExit(count);
            //检查成绩是否合规
            checkExamScore(details.getScore());
            //处理分数评级
            parseExamRating(details);
            details.setCreateTime(DateUtils.getNowDate());
            SysUser user = userMapper.selectUserById(details.getUserId());
            return detailsMapper.insertDetails(details);
        }else{
            return 0;
        }
    }

    private void checkExamIsExit(int count) {
        // 抛出存在异常
        if (count != 0) {
            throw new BaseException("成绩已经存在");
        }
    }

    private void checkExamScore(Double score) {
        // 抛出存在异常
        // 验证 分数是否合格 > 0
        if (score<0) {
            // 抛出异常
            throw new BaseException("分数不合规");
        }
    }

    private void parseExamRating(Details details) {
        // 分数评级 redis 获取分数等级
        List<SysDictData> examRating = dataMapper.selectDictDataByType("ability_exam_rating");
        for (SysDictData sysDictData : examRating) {
            if(Arith.sub(details.getScore(),Double.valueOf(sysDictData.getDictValue()))>=0){
                details.setRating(sysDictData.getDictLabel());
                break;
            }
        }
    }

    /**
     * 修改考试详情
     *
     * @param details 考试详情
     * @return 结果
     */
    @Override
    public int updateDetails(Details details) {
        //检查成绩是否合规
        checkExamScore(details.getScore());

        //处理分数评级
        parseExamRating(details);
        details.setUpdateTime(DateUtils.getNowDate());
        return detailsMapper.updateDetails(details);
    }

    /**
     * 批量删除考试详情
     *
     * @param detailsIds 需要删除的考试详情ID
     * @return 结果
     */
    @Override
    public int deleteDetailsByIds(Long[] detailsIds) {
        return detailsMapper.deleteDetailsByIds(detailsIds);
    }

    /**
     * 删除考试详情信息
     *
     * @param detailsId 考试详情ID
     * @return 结果
     */
    @Override
    public int deleteDetailsById(Long detailsId) {
        return detailsMapper.deleteDetailsById(detailsId);
    }

    /**
     * 获取未录入人员信息
     *
     * @param details 考试记录ID
     * @return BeiBenUser 结果
     */
    @Override
    public List<BeiBenUser> selectUsersByRecordId(Details details) {
        if(null!=details.getDeptId()){
            return beiBenUserMapper.selectDeptByDeptId(details.getDeptId());
        }else{
            return detailsMapper.selectUsersByRecordId(details.getRecordId());
        }
    }
}
